from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.views import Response
from myapp.models import  *
from myapp.serializers import *

class login(APIView):

    def post(self,request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        user= User.objects.filter(username=username,password=password).first()
        if user:
            return Response({'code':200,'msg':'登录成功','uid':user.id})
        else:
            return Response({'code': 403, 'msg': '登录失败'})

class Show_ding(APIView):
    def get(self,request):
        uid= request.GET.get('uid')
        ding = Ding.objects.filter(uid=uid).all()
        dingser=  Ding_ser(ding,many=True)

        goods = Goods.objects.all()
        goods_ser= Goods_ser(goods,many=True)

        user = User.objects.all()
        user_ser= User_ser(user,many=True)

        ding_info = Ding_Info.objects.all()
        ding_info_ser = Ding_Info_ser(ding_info, many=True)
        return Response({"code":200,'data':dingser.data,'goods':goods_ser.data,'users':user_ser.data,'ding_infos':ding_info_ser.data})

class Inser_comment(APIView):
    def post(self,request):
        print(request.POST)
        did = request.POST.get('did')
        data={
            'uid' : request.POST.get('uid'),
            'gid': request.POST.get('gid'),
            'content': request.POST.get('content'),
        }
        # uid = request.POST.get('uid')
        # gid=request.POST.get('gid')
        # content= request.POST.get('content')
        comment = Comment_ser(data=data)
        print(comment.is_valid())
        if comment.is_valid():
            comment.save()
            ding = Ding.objects.get(pk = did)
            print(ding)
            ding.is_comment = 1
            ding.save()
        return Response({"code":200,'msg':'评论成功'})


class Show_good(APIView):
    def get(self,request):
        return Response({'code':200})